package net.oschina.gitapp.ui.basefragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.gitapp.R;
import net.oschina.gitapp.adapter.ViewPageFragmentAdapter;
import net.oschina.gitapp.widget.PagerSlidingTabStrip;

/**
 * 带有导航条的基类
 */
public abstract class BaseViewPagerFragment extends BaseFragment {

    protected PagerSlidingTabStrip mTabStrip;//自定义滑动选项卡View
    protected ViewPager mViewPager;//分页
    protected ViewPageFragmentAdapter mTabsAdapter;//适配器对象

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        //初始化自定义的选项卡View
        return inflater.inflate(R.layout.base_viewpage_fragment, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //创建该Fragment的视图
        mTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.pager_tabstrip);

        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        //获取一个适配器对象
        mTabsAdapter = new ViewPageFragmentAdapter(getChildFragmentManager(),
                mTabStrip, mViewPager);
        //适配器操作
        onSetupTabAdapter(mTabsAdapter);
        mTabsAdapter.notifyDataSetChanged();
        mViewPager.setOffscreenPageLimit(3);//设置偏移屏风页面限制
        if (savedInstanceState != null) {
            int pos = savedInstanceState.getInt("position");
            mViewPager.setCurrentItem(pos, true);//设置当前项目
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("position", mViewPager.getCurrentItem());//保存当前的分页指针
        Log.i("","//保存当前的分页指针" + mViewPager.getCurrentItem());
    }

    protected abstract void onSetupTabAdapter(ViewPageFragmentAdapter adapter);
}